﻿using System;
using System.Windows;
using PersonalHealth.Data.IService;
using PersonalHealth.Data.Service;
using PersonalHealth.Data.Model;
using UI.Comm.Controls;
using UI.Comm.Common;
using HealthManagement.Common;

namespace HealthManagement.Views
{
    /// <summary>
    /// UserForm.xaml 的交互逻辑
    /// </summary>
    public partial class UserForm : PMWindow
    {
        MeasureUser user = new MeasureUser() { Id = 0 };
        public event Action<int> onSaveHandler;
        public UserForm()
        {
            InitializeComponent();

            ISexService sexSer = new SexService();
            
            Sex.ItemsSource = sexSer.Find(c => 1 == 1);//.Select(c => c.Name);

            IBloodTypeService btSer = new BloodTypeService();
            BloodType.ItemsSource = btSer.Find(c => 1 == 1);//.Select(c => c.Name);

            this.DataContext = user;

        }

        public UserForm(Action<int> a) : this()
        {
            onSaveHandler = a;

        }

        public UserForm(MeasureUser user) 
        {
            InitializeComponent();

            ISexService sexSer = new SexService();

            Sex.ItemsSource = sexSer.Find(c => 1 == 1);//.Select(c => c.Name);

            IBloodTypeService btSer = new BloodTypeService();
            BloodType.ItemsSource = btSer.Find(c => 1 == 1);//.Select(c => c.Name);

            this.DataContext = this.user = user;
            tbPsd.Password = tbPsdConfirm.Password = user.Password;
        }

        public UserForm(MeasureUser user, Action<int> a)
            : this(user)
        {
            onSaveHandler = a;
        }

        public bool IsUserTest { get; set; }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            IUserService ser = new UserService();

            if (string.IsNullOrEmpty(tbPsd.Password))
            {
                tbPsd.Password = "";
            }
            if (string.IsNullOrEmpty(tbPsdConfirm.Password))
            {
                tbPsdConfirm.Password = "";
            }
            if (tbPsd.Password != tbPsdConfirm.Password)
            {
                MessageBox.Show("密码输入不一致请重新输入！");
                tbPsd.Clear();
                tbPsdConfirm.Clear();
                tbPsd.Focus();
                return;
            }
            user.Password = tbPsd.Password;

            if (user.Id == 0)
            {
                if (string.IsNullOrEmpty(user.UserName))
                    user.UserName = "匿名用户";

                if (IsUserTest)
                {
                    user.UserTypeId = (int)UserType.TestUser;
                }
                else
                {
                    user.UserTypeId = (int)UserType.MeasureUser;
                }
                user.CreateDate = user.ModifyDate = DateTime.Now;
                ser.Register(user);

                Tools.ShowMessage(true, string.Format("新增成功，服务编号为：{0}", user.ServiceNo));
            }
            else
            {
                user.ModifyDate = DateTime.Now;
                ser.Update(user);
            }
            this.Close();
            if (onSaveHandler != null)
                onSaveHandler(user.Id);
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

    }
}
